using System;
using System.Collections.Generic;
using System.Text;
using Chat.Util;

namespace Chat.Client
{
    class MessageReceive
    {


        public static User getUser(byte[] bUser)
        {
            int len = bUser.Length;
            User u = new User();
            byte[] bIp = Tools.getBytes(bUser, 0, Config.IP_SIZE);
            byte[] bId = Tools.getBytes(bUser, Config.IP_SIZE, Config.ID_SIZE);
            byte[] bName = Tools.getBytes(bUser, Config.ID_SIZE + Config.IP_SIZE, Config.NAME_SIZE);
            u.ip = Tools.getIp(bIp);
            u.id = Tools.getString(bId);
            u.name = Tools.getString(bName);
            return u;
        }
        
        public static User[] getUserList(byte[] bBody)
        {
            int len = bBody.Length;
            int L = len / Config.USER_SIZE;
            User[] ul = new User[L];

            for (int i = 0; i < L; i++)
            {
                byte[] bUser = Tools.getBytes(bBody,i*Config.USER_SIZE,Config.USER_SIZE);
                ul[i] = getUser(bUser);
            }
            return ul;
        }

        public static FromMsg getFromString(Head h, byte[] bBody)
        {
            FromMsg fm = new FromMsg();
            fm.fromId = h.id;
            fm.fromIp = h.ip;
            fm.fromName = h.name;
            fm.msg = Tools.getString(bBody);
            return fm;
        }

        public static ToMsg getSendString(Head h,byte[] bBody)
        {
            ToMsg tm = new ToMsg();
            tm.toId = h.id;
            tm.toIp = h.ip;
            tm.toName = h.name;
            tm.msg = Tools.getString(bBody);
            return tm;
        }


    }
}
